package com.koron.standard.gradeStock.controller;

import cn.hutool.poi.excel.WorkbookUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.koron.bean.base.Response;
import com.koron.common.core.business.common.service.TOrderNoMaxService;
import com.koron.standard.bean.query.PageQuery;
import com.koron.standard.excel.SameRowMargeStrategy;
import com.koron.standard.gradeStock.bean.dto.GradeDTO;
import com.koron.standard.gradeStock.bean.excel.GradeExcel;
import com.koron.standard.gradeStock.bean.query.GradeQuery;
import com.koron.standard.gradeStock.bean.vo.GradeVO;
import com.koron.standard.gradeStock.excel.GradeExcelListener;
import com.koron.standard.gradeStock.service.GradeItemService;
import com.koron.standard.gradeStock.service.GradeService;
import com.koron.standard.patrol.bean.excel.PatrolExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

/**
 * 大修分级分项验收标准(Grade)表控制层
 *
 * @author jzp
 * @since 2021-09-26
 */
@Api(tags = "大修分级分项验收标准接口")
@RestController
@RequestMapping("grade")
public class GradeController {

    @Autowired
    private GradeService gradeService;

    @Autowired
    private GradeItemService gradeItemService;

    @Autowired
    TOrderNoMaxService tOrderNoMaxService;

    @ApiOperation(value = "分页查询")
    @GetMapping("page")
    public Response<Page<GradeVO>> page(PageQuery<GradeVO> pageQuery, GradeQuery query) {
        Page<GradeVO> page = gradeService.page(pageQuery, query);
        return Response.succ(page, page.getTotal());
    }

    @ApiOperation(value = "列表查询")
    @GetMapping("list")
    public Response<List<GradeDTO>> list(GradeQuery query) {
        return Response.ok(gradeService.list(query));
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping
    public Response<GradeDTO> select(@RequestParam String id) {
        GradeDTO gradeDTO = gradeService.getDtoById(id);
        return Response.ok(gradeDTO);
    }

    @ApiOperation(value = "保存")
    @PostMapping
    public Response save(@RequestBody GradeDTO gradeDTO) {
        gradeService.saveDto(gradeDTO);
        return Response.ok(gradeDTO.getId());
    }

    @ApiOperation(value = "修改")
    @PutMapping
    public Response update(@RequestBody GradeDTO gradeDTO) {
        gradeService.updateDtoById(gradeDTO);
        return Response.ok();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping
    public Response delete(@RequestParam("ids") List<String> ids) {
        gradeService.removeByIds(ids);
        return Response.ok();
    }

    @ApiOperation(value = "设置状态")
    @PutMapping("updateStatus")
    public Response updateStatus(@RequestParam String id, @RequestParam Integer status) {
        gradeService.updateStatus(id, status);
        return Response.ok();
    }

    @ApiOperation(value = "设置为公共标准")
    @PutMapping("updateToPublic")
    public Response updateToPublic(@RequestParam("ids") List<String> ids) {
        gradeService.updateToPublic(ids);
        return Response.ok();
    }


    @SneakyThrows
    @ApiOperation(value = "下载模板")
    @GetMapping("template")
    public void template(HttpServletResponse response) {
        String path = "excel/grade_standard.xlsx";
        ClassPathResource classPathResource = new ClassPathResource(path);
        Workbook book = WorkbookUtil.createBook(classPathResource.getInputStream());
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("分级分项验收", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        book.write(response.getOutputStream());
    }

    @SneakyThrows
    @ApiOperation(value = "导入")
    @PostMapping("import")
    public Response importFile(@RequestParam("file") MultipartFile file) {
        EasyExcel.read(file.getInputStream(), PatrolExcel.class, new GradeExcelListener(gradeService, gradeItemService, tOrderNoMaxService))
                .sheet("分级分项验收")
                .headRowNumber(2)
                .doRead();

        return Response.ok("导入成功", null);
    }

    @SneakyThrows
    @ApiOperation(value = "导出", notes = "根据条件导出查询出的所有数据")
    @GetMapping("export")
    public void exportFile(HttpServletResponse response, GradeQuery query) {
        String path = "excel/grade_standard_export.xlsx";
        ClassPathResource classPathResource = new ClassPathResource(path);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("分级分项验收", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<GradeExcel> gradeExcels = gradeService.listExcel(query);
        EasyExcel.write(response.getOutputStream())
                .withTemplate(classPathResource.getInputStream())
                // .needHead(false)
                .sheet("分级分项验收")
                .registerWriteHandler(new SameRowMargeStrategy<GradeExcel>(gradeExcels, new HashSet<>(Arrays.asList(0, 1, 2)), GradeExcel::getTypeId))
                .doFill(gradeExcels);
    }

}
